package com.sooncode.project.focusservice.controller;

import com.sooncode.project.core.finder.Finder;
import com.sooncode.project.focusservice.model.Member;
import com.sooncode.project.focusservice.model.Test;
import com.sooncode.project.focusservice.service.DownloadCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("update")
@RefreshScope
public class UpdateController {
    @Value("${focusUpdate.ver}")
    private int appVar;
    @Value("${focusUpdate.appUrl}")
    private String appUrl;
    @Value("${focusUpdate.updateUrl}")
    private String updateUrl;
    @Autowired
    DownloadCourseService downloadCourseService;
    @GetMapping("update")
    public Map update(){
        Map<String,Object> map=new HashMap<>();
        map.put("ver",appVar);
        map.put("appUrl",appUrl);
        map.put("updateUrl",updateUrl);
        return map;
    }

    @PostMapping("age")
    public boolean updateAge(){
        List<Member> memberList=new Finder<>(Member.class).list();
        for(Member member:memberList)
            try {
                System.out.println("更新:"+member.getName());
                member.updateAge();
            }catch (Exception e){
            }
        List<Test> testList=new Finder<>(Test.class).list();
         for(Test test:testList){
             try {
                 System.out.println("更新测试:"+test.getName());
                 Member member = new Finder<>(Member.class).byId(test.getMemberId());
                 test.setAge(member.getAge());
                 test.setShowAge(member.getShowAge());
                 test.update();
             }catch (Exception ex){

             }
        }
        return true;
    }
    @GetMapping("publicApi/checkCourse")
    public void checkCourse(HttpServletResponse response){
        downloadCourseService.downloadCheckCourse(response);
    }

}
